package com.fenghongzhang.kotlin1903

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : AppCompatActivity() {

    private var t:Int = 5

    private lateinit var timer:Timer
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //倒计时的写法
        timer = Timer()
        timer.schedule(object:TimerTask(){
           override fun run() {
               runOnUiThread{
                   t--
                   time.text = t.toString()
                   if(t == 0){
                       cancel()
                       //跳转
                       val intent = Intent(this@MainActivity, IndexActivity::class.java)
                       startActivity(intent)
                   }
               }
           }
       },0,1000)



        img.setOnClickListener{
            val intent = Intent(this, IndexActivity::class.java)
            startActivity(intent)
            timer.cancel()
            finish()
        }


    }
}
